<?php 
error_reporting(0);
define('PATH', substr(dirname(__FILE__), 0, -13));
$cfg_config = include PATH . '/config/config.php';
include PATH . '/core/Mysql.class.php';

$size = 10;
$kid = isset($argv[1])?$argv[1]:0;


$db = new dbQuery($cfg_config['mysql']['hostname'], $cfg_config['mysql']['username'], $cfg_config['mysql']['password'], $cfg_config['mysql']['database']);
$db->connect($cfg_config['mysql']['hostname'], $cfg_config['mysql']['username'], $cfg_config['mysql']['password'], $cfg_config['mysql']['database']);

while (true) {
	$sql = "select * from `video_keys` where  `kid`> $kid and `sgk`='' ORDER BY `kid` ASC  limit $size" ;
    $rr = $db -> query($sql);
    $uitems = array();
    while($row = $db->fetch_array($rr)){
		$tkey = $row['key'];
        $kid = $row['kid'];
		$uitems["http://wap.sogou.com/web/searchList.jsp?keyword=".urlencode($tkey)."&pg=webSearchList"] = $kid;
    }
	$start = $kid;
    $res = classic_curl(array_keys($uitems), 1);
	foreach($res as $url => $keys) {
		if (count($keys) > 0) {

			$kid = $uitems[$url];
			$kkk = addslashes(implode(',',$keys));

			$sql = "UPDATE `video_keys` set `sgk` = '{$kkk}' where `kid` = {$kid}";
			$db -> query($sql);
			echo "\r $kid  (" . count($keys) . ") ok...              \n";
		} else{
			echo "\r   $kid  err ...              \n";
		}
	} 
} 

// 析出视频数据
function get_sgk($txt) {
    //if(preg_match_all('%<a href="searchList.jsp[^>]+">([^<]+)</a>%',$txt,$arr)){
    if(preg_match_all("/<a href=\"\?keyword=[^>]+\"([^<]+)<\/a>/",$txt,$arr)){
		$keys = $arr[1];
		return $keys ;
	}else{
		return null;
	}
	
} 
// 并发采集
function classic_curl($urls, $delay) {
	$queue = curl_multi_init();
	$map = array();
	foreach ($urls as $url) {
		// create cURL resources
		$ch = curl_init(); 
		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $url);

		curl_setopt($ch, CURLOPT_TIMEOUT, 8);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_NOSIGNAL, true); 
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us)AppleWebKit/532.9(KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7");
		// add handle
		curl_multi_add_handle($queue, $ch);
		$map[$url] = $ch;
	} 
	$active = null; 
	// execute the handles
	do {
		$mrc = curl_multi_exec($queue, $active);
	} while ($mrc == CURLM_CALL_MULTI_PERFORM);

	while ($active > 0 && $mrc == CURLM_OK) {
		if (curl_multi_select($queue, 0.5) != -1) {
			do {
				$mrc = curl_multi_exec($queue, $active);
			} while ($mrc == CURLM_CALL_MULTI_PERFORM);
		} 
	} 
	$responses = array();
	foreach ($map as $url => $ch) {
		if ($delay == 0) {
			$responses[$url] = get_sgk(curl_multi_getcontent($ch));
		} else {
			$responses[$url] = get_sgk(curl_multi_getcontent($ch));
		} 

		curl_multi_remove_handle($queue, $ch);
		curl_close($ch);
	} 
	curl_multi_close($queue);
	return $responses;
} 
